package com.haoran.dwz.domain.entities;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.ConstraintMode;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.ForeignKey;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonManagedReference;

@Entity
@Table(name="t_permission")
public class Permission extends AbstractEntity {
	
	/**
	 * 编码前缀
	 */
	public static String PREFIX_OF_NUMBER = "p_";
	/**
	 * 
	 */
	private static final long serialVersionUID = 8581140638871641542L;

	/** 资源名称 */
	@Column(unique=true)
	private String name;
	
	@Column(unique=true,nullable=false)
	private String number;
	/**
	 * 将来控制按钮是否显示的
	 */
	@Column(nullable=true)
	private String auth;

	/** url地址 */
	@Column(unique=false)
	private String url;
	
	/** url地址 */
	@Column(name="http_method")
	private String httpMethod;
	
	@Column(name="type" ,columnDefinition="integer COMMENT '0:目录; 1: 菜单; 2: 功能; 3: 按钮; 4: 资源;' ")
    @Enumerated(EnumType.ORDINAL)  
    private PermissionType permissionType = PermissionType.CATALOG;

	/** 描述 */
    @Column(name="description")
	private String description;
	
	/**
	 * 
	 */
	@Column(name="order_num")
	private int orderNumber;
	
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "parent_id",foreignKey = @ForeignKey(name = "none",value = ConstraintMode.NO_CONSTRAINT))
	@JsonBackReference
	private Permission parent;

	@SuppressWarnings("deprecation")
	@OneToMany(fetch = FetchType.EAGER, mappedBy = "parent",orphanRemoval=true)
	@JsonManagedReference  
	@org.hibernate.annotations.ForeignKey(name = "none")
	private List<Permission> children = new ArrayList<Permission>(0);
	
	@JsonBackReference  
    @ManyToMany(mappedBy = "permissions", fetch = FetchType.EAGER)
    private Set<Role> roles = new HashSet<Role>();

    
	public String getHttpMethod() {
		return httpMethod;
	}

	public void setHttpMethod(String httpMethod) {
		this.httpMethod = httpMethod;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public PermissionType getPermissionType() {
		return permissionType;
	}

	public void setPermissionType(PermissionType permissionType) {
		this.permissionType = permissionType;
	}

	public Permission getParent() {
		return parent;
	}

	public void setParent(Permission parent) {
		this.parent = parent;
	}

	public List<Permission> getChildren() {
		return children;
	}

	@JsonBackReference
	public void setChildren(List<Permission> children) {
		this.children = children;
	}

	public Set<Role> getRoles() {
		return roles;
	}

	public void setRoles(Set<Role> roles) {
		this.roles = roles;
	}

	public int getOrderNumber() {
		return orderNumber;
	}

	public void setOrderNumber(int orderNumber) {
		this.orderNumber = orderNumber;
	}

	public String getAuth() {
		return auth;
	}

	public void setAuth(String auth) {
		this.auth = auth;
	}

	public String getNumber() {
		return number;
	}

	public void setNumber(String number) {
		this.number = number;
	}

	@Override
	public String toString() {
		return "Permission [name=" + name + ", number=" + number + ", auth=" + auth + ", url=" + url
				+ ", permissionType=" + permissionType + ", description=" + description + ", orderNumber=" + orderNumber
				+ "]";
	}

	/**
	 * 权限许可证类型
	 */
	public static enum PermissionType {
		/**
		 * 0：目录
		 */
		CATALOG,
		
		/**
		 * 1：菜单
		 */
		MENU,
        
		/**
		 * 2：功能
		 */
        FUNCTION,
        
        /**
         * 3：按钮
         */
        BUTTON,
        
        /**
         * 4：资源
         */
        RESOURCE;
    }
}
